package de.hdu.pms.ho.dao;


import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import de.hdu.pms.ho.model.Kategorie;
import de.hdu.pms.ho.model.Zimmer;



/**
 * Class for all necessary functions with {@link Zimmer} content
 * @author Helena Semmel
 *
 */
public class ZimmerDao extends HibernateDaoSupport{
	
	public static void setLog(Logger log) {
		Log = log;
	}

	public static Logger Log = Logger.getLogger(ZimmerDao.class);
	
	
	/**
	 * Returns a single room by its primary key
	 * @param id the primary key of a {@link Zimmer}
	 * @return a single Zimmer
	 */
	public Zimmer findById(Integer id) {
		HibernateTemplate template = getHibernateTemplate();	//damit wird session aufgebaut, sobald get-Methode
		return (Zimmer) template.get(Zimmer.class, id);			//ausgeführt worden ist, wird session geschlossen
		
	}
	
	/**
	 * Returns all rooms from the database
	 * @return all {@link Zimmer} objects
	 */
	public List<Zimmer> findAll() {
		HibernateTemplate template = getHibernateTemplate();
		List<Zimmer> results = template.loadAll(Zimmer.class);
		return (List<Zimmer>) results;
	}
	
	
	/**
	 * Saves a specified object in the database - a new one, or updates an existing object
	 * @param zimmer an object of {@link Zimmer} that should be saved
	 * @return the {@link Zimmer} object
	 */
	public Zimmer save(Zimmer zimmer) {
		HibernateTemplate template = getHibernateTemplate();
		template.saveOrUpdate(zimmer);
		return zimmer;
	}

	/**
	 * Deletes a specified object in the database
	 * @param zimmer the object that should be deleted
	 */
	public void delete(Zimmer zimmer) {
		HibernateTemplate template = getHibernateTemplate();
		template.delete(zimmer);
	}
	
	
	/**
	 * Deletes a specified object in the database
	 * @param id primary key of the object that should be deleted
	 */
	public void deletebyId(Integer id){								//aufgrund der NestedServletException
		HibernateTemplate template = getHibernateTemplate();
		Zimmer z = findById(id);
		template.delete(z);
		template.flush();
	}
	
	
	/**
	 * Creates a new {@link Zimmer} and saves it in the database.
	 * @param nr the room number
	 * @param betten the number of beds
	 * @param qm the square meter
	 * @param haustier if pets are allowed
	 * @param raucher if smoking is allowed
	 * @param kategorie the object of {@link Kategorie}
	 */
	public void create(Integer nr, Integer betten, Integer qm, Boolean haustier, Boolean raucher, Kategorie kategorie){
		HibernateTemplate template = getHibernateTemplate();
		Zimmer zimmer = new Zimmer(nr, betten, qm, haustier, raucher, kategorie);
		template.persist(zimmer);
		//return zimmer.getId();
	}
	
	/**
	 * Modifies an excisting object of {@link Zimmer} and saves it in the database
	 * @param id the primary key of {@link Zimmer}
	 * @param nr the new room number
	 * @param betten the new number of beds
	 * @param qm the new square meter 
	 * @param haustier if pets are allowed
	 * @param raucher if smoking is allowed
	 * @param kategorie the object of {@link Kategorie}
	 */
	public void modify(Integer id, Integer nr, Integer betten, Integer qm, Boolean haustier, Boolean raucher, Kategorie kategorie) { 	
		HibernateTemplate template = getHibernateTemplate();
		//Session session = template.getSessionFactory().openSession();
		Zimmer zimmer = (Zimmer)template.get(Zimmer.class, id);
		//session.beginTransaction();
		zimmer.setNr(nr);
		zimmer.setBetten(betten);
		zimmer.setQm(qm);
		zimmer.setHaustier(haustier);
		zimmer.setRaucher(raucher);
		zimmer.setKategorie(kategorie);
		template.saveOrUpdate(zimmer);
	}
	
	
}
